<template>
  <div class="breadcrumb">
    <el-breadcrumb class="app-breadcrumb" separator="/">
      <el-breadcrumb-item v-for="item in levelList" :key="item.path">
        <!-- <span v-if="item.redirect === 'noRedirect' || index == levelList.length - 1" class="no-redirect">{{ item.meta.title }}</span>
				<a v-else @click.prevent="handleLink(item)">{{ item.meta.title }}</a> -->
        <span class="no-redirect">
          {{ item.meta.title }}
        </span>
      </el-breadcrumb-item>
    </el-breadcrumb>
  </div>
</template>

<script>
import pathToRegexp from "path-to-regexp";

export default {
  data() {
    return {
      levelList: null,
      toRouterTtitle: "",
    };
  },
  watch: {
    $route() {
      this.getBreadcrumb();
    },
  },
  computed: {
    key() {
      return this.$route.path;
    },
  },
  created() {
    this.getBreadcrumb();
  },
  methods: {
    getBreadcrumb() {
      // only show routes with meta.title
      let matched = this.$route.matched.filter((item) => {
        if (localStorage.getItem("SYS_TYPE") == "SS2" && item.meta.title == '鱼塘块生产信息库') {
			item.meta.title = "地塘块生产信息库";
			item.meta.title = "地塘块生产信息库";
        }
		console.log(item.meta.title)
        return item.meta && item.meta.title;
      });
      const first = matched[0];

      if (!this.isDashboard(first)) {
        localStorage;
        matched = [
          { path: "/", meta: { title: this.$store.getters.systemSplit.title } },
        ].concat(matched);
      }

      this.levelList = matched.filter(
        (item) => item.meta && item.meta.title && item.meta.breadcrumb !== false
      );
      this.toRouterTtitle = matched[matched.length - 1].meta.title;
    },
    isDashboard(route) {
      const name = route && route.name;
      if (!name) {
        return false;
      }
      return name.trim().toLocaleLowerCase() === "/".toLocaleLowerCase();
    },
    pathCompile(path) {
      const { params } = this.$route;
      var toPath = pathToRegexp.compile(path);
      return toPath(params);
    },
    handleLink(item) {
      const { redirect, path } = item;
      if (redirect) {
        this.$router.push(redirect);
        return;
      }
      this.$router.push(this.pathCompile(path));
    },
  },
};
</script>

<style lang="scss" scoped>
.breadcrumb {
  background: #fff;
  border-bottom: 1px solid #ddd;
  padding: 20px 10px;
  .toRouterTtitle {
    margin-top: 10px;
    font-size: 16px;
    font-weight: 500;
  }
}
.app-breadcrumb.el-breadcrumb {
  display: inline-block;
  font-size: 14px;
  .no-redirect {
    color: #333;
    cursor: text;
    font-weight: bold;
  }
}
</style>
